package co.fitstart.admin.web.controller.finance;

import java.math.BigDecimal;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;

import co.fitstart.admin.service.DataService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.finance.ExchangeRate;

@Controller
@RequestMapping("/exchange-rates")
@MenuSetting("menu-finance")
public class ExchangeRateController {
    
    @Autowired
    @Qualifier("dataService")
    private DataService<ExchangeRate> exchangeRateService;
    
    private Logger logger = LoggerFactory.getLogger(ExchangeRateController.class);
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    @MenuSetting("submenu-exchange-rate-management")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("finance/exchange-rate-index");
        
        List<ExchangeRate> exchangeRates = exchangeRateService.listAll(ExchangeRate.class);
        ExchangeRate exchangeRate = exchangeRates.isEmpty() ? null : exchangeRates.get(0);
        
        modelAndView.addObject("exchangeRate", exchangeRate);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param exchangeRateId
     * @return
     */
    @RequestMapping(value = "/{exchangeRateId}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("exchangeRateId") Long exchangeRateId) {
        ModelAndView modelAndView = new ModelAndView("finance/exchange-rate-edit");
        
        ExchangeRate exchangeRate = exchangeRateService.load(ExchangeRate.class, exchangeRateId);
        
        modelAndView.addObject("exchangeRate", exchangeRate);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param exchangeRateId
     * @param rate
     * @return
     */
    @RequestMapping(value = "/{exchangeRateId}", method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(@PathVariable("exchangeRateId") Long exchangeRateId,
                                       @RequestParam("exchangeRate") BigDecimal rate) {
        
        Result result = new Result();
        
        try {
            
            ExchangeRate exchangeRate = exchangeRateService.load(ExchangeRate.class, exchangeRateId);
            exchangeRateService.update(exchangeRate.edit(rate));
            
            result.success().message("修改汇率成功");
            
        } catch (Exception e) {
            
            result.fail().message("修改汇率失败");
            
            logger.error("#onEdit() error ", e);
            
        }
        
        return JsonUtils.toJsonString(result);
    }

}
